#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 
#include <strings.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>

void error(char *msg)
{
    perror(msg);
    exit(0);
}

/*
	Funcao que recebe o vetor[1000] do ultimo nivel p/ impressao
*/
void impressao(char* port)
{
	struct timeval inicio, final;
 	long temposort;
	int sockfd, newsockfd, portno, clilen;
	int bufferfinal[1000],i,flag=0;
	struct sockaddr_in serv_addr, cli_addr;
	int n;

	sockfd = socket(AF_INET, SOCK_STREAM, 0); /* Configuraçao do socket */
	if (sockfd < 0) 
		error("ERROR opening socket\n");
	bzero((char *) &serv_addr, sizeof(serv_addr));
	portno = atoi(port);
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = INADDR_ANY;
	serv_addr.sin_port = htons(portno);
	if (bind(sockfd, (struct sockaddr *) &serv_addr,
		sizeof(serv_addr)) < 0) 
		error("ERROR on binding\n");
	listen(sockfd,5);
	clilen = sizeof(cli_addr);

	/* Modo Server esperando conexao Nivel 4 */
	
	while(flag<1)
	{
		printf("vtc");
		/* Cria conexao */
		newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
		if (newsockfd < 0) 
			error("ERROR on accept\n");
		else if(newsockfd > 0)
		{
			if(flag==0)
				gettimeofday(&inicio,NULL);
			printf("Nova Conexao\n");
			n = read(newsockfd,bufferfinal,sizeof(bufferfinal));
			flag++;
		}
		
		if (n < 0) error("ERROR reading from socket\n");
		close(newsockfd);
	}	
	gettimeofday(&final, NULL);
	temposort = (final.tv_sec-inicio.tv_sec)*1000000 + final.tv_usec-inicio.tv_usec; /* Tempo calculado da comunicaçao com o Nivel 4 */
	printf("tempo comunicacao(ms): %lu\n", temposort);  

	//for(i=0;i<1000;i++)
		//printf("\n%d Indice %d",bufferfinal[i],i);
}

int main(int argc, char *argv[])
{
	struct timeval inicio, final;
 	long tempototal;
	int sockfd, portno, n;
	int numero,i;
	int buffer[125];

	struct sockaddr_in serv_addr;
	struct hostent *server;

	srand ( time(NULL) );

	

	if (argc < 4) {
		fprintf(stderr,"usage %s hostname port\n", argv[0]);
		exit(0);
	}
	portno = atoi(argv[3]);
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd < 0) 
		error("ERROR opening socket\n");
	server = gethostbyname(argv[2]);
	if (server == NULL) {
		fprintf(stderr,"ERROR, no such host\n");
		exit(0);
	}

	bzero((char *) &serv_addr, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	bcopy((char *)server->h_addr, 
	(char *)&serv_addr.sin_addr.s_addr,	
	server->h_length);
	serv_addr.sin_port = htons(portno);
	gettimeofday(&inicio,NULL);
	/* Conexao feita */
	if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) 
		error("ERROR connecting\n");
	bzero(buffer,125);
	
	for(i=0;i<125;i++)
		buffer[i]=rand()/1000;

	//for(i=0;i<125;i++)
		//printf("\n%d Indice %d",buffer[i],i);	
	   
	printf("Enviando valores\n");	
	/*Envio do vetor p/ nivel superior */
	n = write(sockfd,buffer,sizeof(buffer));
	if (n < 0) 
		error("ERROR writing to socket\n");
	shutdown(sockfd,2);
	impressao(argv[1]);

	gettimeofday(&final, NULL);
	tempototal = (final.tv_sec-inicio.tv_sec)*1000000 + final.tv_usec-inicio.tv_usec;
	printf("\ntempo total(ms): %lu\n", tempototal); /* Tempo calculado de todo o processo */  

	printf("\n\n");
	return 0;
}
